﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KaoWuYou.Models;
using KaoWuYouLibs.Bll;

namespace KaoWuYou.Controllers
{
    [HandleError]
    public class WebUserController : Controller
    {
        PersonalInfomationBusiness personalinfo = new PersonalInfomationBusiness();
        //
        // GET: /WebUser/

        // **************************************
        // URL: /WebUser/UpdateUserInfo
        // **************************************

        public ActionResult UpdateUserInfo()
        {            
            return View();            
        }

        [HttpPost]
        public ActionResult UpdateUserInfo(WebUserModel model)
        {
            if (ModelState.IsValid)
            {
                // 更新用户信息成功，跳转至“成功”页面
                if (model.UpdateUserInfo())
                {
                    return View("Success");
                }
                // 更新用户信息失败，跳转至“失败”页面
                else
                {
                    return View("Error");
                }
            }

            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            return View("Error");
        }

        // **************************************
        // URL: /WebUser/UpdatePassword
        // **************************************

        public ActionResult UpdatePassword()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UpdatePassword(WebUserModel model)
        {
            if (ModelState.IsValid)
            {   
                // 更新用户密码成功，跳转至“成功”页面
                if (model.UpdatePassword())
                {
                    return View("Success");
                }
                // 更新用户密码失败，跳转至“失败”页面
                else
                {
                    return View("Error");
                }
            }

            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            return View("Error");
        }

        // **************************************
        // URL: /WebUser/HistoryReview
        // **************************************

        public ActionResult HistoryReview()
        {
            return View();
            throw new NotImplementedException();
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            try
            {
                personalinfo.RegisterUser(model.AccountName, model.Password, model.Name, model.Tel, model.Email, model.Gender);
                return new RedirectResult("");
            }
            catch
            {
                return new RedirectResult("");
            }
        }



        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Login()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Login(LoginModel model)
        {
            if (personalinfo.LogIn(model.AccountName, model.Password))
            {
                Session["Authority"] = personalinfo.GetPersonalInformation(model.AccountName, model.Password).Authority;
                Session["Name"] = model.AccountName;
            }
            return View();
        }

        public ActionResult Logout()
        {
            Session["Authority"] = null;
            Session["Name"] = null;
            Session["Paper"] = null;
            return RedirectToAction("Index", "Home");
        }
    }
}
